package com.itheima.guozi2;

public class Foodie implements Runnable {
private Desk desk;

    public Foodie(Desk desk) {
        this.desk=desk;

    }


    @Override
    public void run() {
        //吃
        // 死循环
        while (true) {
            synchronized (desk.getObj()) {
                //看看到数了吗
                if (desk.getCount()> 0) {
                    //没到
                    //看看有没有
                    if (desk.isFlag()) { // 有 直接吃 吃完叫继续做
                        desk.setCount(desk.getCount()-1);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"吃了一个煎饼");
                        desk.setFlag(false);
                        desk.getObj().notifyAll();
                    }else {
                        //没有 等着
                        try {
                            desk.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                } else {
                    System.out.println("老板已经撤摊了 ,换一家");
                    break;
                }
            }

        }

    }
}
